package minesweeper;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

public class Field extends JPanel {

    //content value of a mine
    public static final int MINE = -1;
    //content value of a field which is empty
    public static final int EMPTY = 0;
    //content value of a closed field (only used for sprite)  -- now also for gangstas code
    public static final int CLOSED = 9;
    public static final int CLOSEDLEFT = 10;
    public static final int CLOSEDLEFTUP = 11;
    public static final int CLOSEDLEFTUPRIGHT = 12;
    public static final int CLOSEDLEFTUPRIGHTDOWN = 13;
    public static final int CLOSEDLEFTUPDOWN = 14;
    public static final int CLOSEDLEFTRIGHT = 15;
    public static final int CLOSEDLEFTRIGHTDOWN = 16;
    public static final int CLOSEDLEFTDOWN = 17;
    public static final int CLOSEDUP = 18;
    public static final int CLOSEDUPRIGHT = 19;
    public static final int CLOSEDUPDOWN = 20;
    public static final int CLOSEDUPRIGHTDOWN = 21;
    public static final int CLOSEDRIGHT = 22;
    public static final int CLOSEDRIGHTDOWN = 23;
    public static final int CLOSEDDOWN = 24;
    
    
    //holds all sprites, the paint-method get all images from this object
    private final Resources res;
    private final Minefield mf;
    //holds the current status of the field
    private Status status;
    //holds the content of the field (number of nearby mines or mine value or empty value)
    private int content;
private int x1,y1;
    public Field(int content, Resources res, Minefield mf, int x1, int y1) {
        status = Status.CLOSED;
        this.content = content;
        this.res = res;
        this.mf = mf;
        this.x1=x1;
        this.y1=y1;
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                onMousePressed(e);

            }
        });
    }

    public int getX1() {
        return x1;
    }

    public int getY1() {
        return y1;
    }

    @Override
    public void paint(Graphics g) {
        Image i = null;
        if (status == Status.CLOSED) {

            int directionsCheck;// index 0=left, index 1=up, index 2=right, index 3=down 
            //Field.EMPTY=0; Field.CLOSED=9
            directionsCheck = mf.checkClosed(this);
            
                i = res.getImage(directionsCheck);//image names for openspaces are named where something is open and closed e.g. everything around closed 9999.jpg; left open, rest closed -> 0999.jpg
            


        } else {
            i = res.getImage(content);
        }
        g.drawImage(i, 0, 0, getWidth(), getHeight(), null);
        //draws a border to mark the single fields
        //g.drawRect(0, 0, getWidth(),getHeight());

    }

    public void onMousePressed(MouseEvent e) {

        openField();

        if (isMine()) {
            mf.mineHit();
        }

    }

    //increases the count of nearby mines
    public void increaseNearbyMineCount() {
        content++;
    }

    //returns if the field is a mine or not
    public boolean isMine() {
        if (content == MINE) {
            return true;
        }
        return false;
    }

    public void openField() {
        //checks if the field is already open
        if (status == Status.OPEN) {
            return;
        }
        //if the field isn't already open, it is opened
        status = Status.OPEN;

        repaint();
    }

    public Status getStatus() {
        return status;
    }

    public int getContent() {
        return content;
    }
}
